package com.yorick;

import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MyQueueServer {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法


    @Test
    public void sendMessage(){
      JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",System.currentTimeMillis());
        jsonObject.put("name","yorick");
        jsonObject.put("msg","Hello, Direct!");
        //将数据绑定路由键：DirectRouting 发送到交换机：DeirectExchange
        rabbitTemplate.convertAndSend("DeirectExchange","DirectRouting",jsonObject);

    }

    @Test
    public void sendTopicMessage1(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",System.currentTimeMillis());
        jsonObject.put("name","Yorick");
        jsonObject.put("msg","Hello, Yorick!");
        rabbitTemplate.convertAndSend("TopicExchange","topic.frist",jsonObject);
    }

    @Test
    public void sendTopicMessage2(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",System.currentTimeMillis());
        jsonObject.put("name","Roman");
        jsonObject.put("msg","Hello, Roman!");
        rabbitTemplate.convertAndSend("TopicExchange","topic.n",jsonObject);
    }

    @Test
    public void sendFanoutMessage(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id",System.currentTimeMillis());
        jsonObject.put("name","roman");
        jsonObject.put("msg","Hello, Fanout!");
        rabbitTemplate.convertAndSend("FanoutExchange",null,jsonObject);
    }
}
